from django.conf import settings
from django.db import models
import core


class City(models.Model):
    name = models.CharField(max_length=255)

    class Meta:
        app_label = core.APP_LABEL
        ordering = ['name'] 

    def __unicode__(self):
        return self.name


class Strategy(models.Model):
    default_value = 1;
    name = models.CharField(max_length=255)
    description = models.TextField()
    
    class Meta:
        app_label = core.APP_LABEL
        
    def __unicode__(self):
        return self.name


class User(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField(max_length=255, default='')
    cities = models.ManyToManyField(City, related_name='citizens')
    participation_raiting = models.IntegerField(max_length=3, default=0)
    organization_raiting = models.IntegerField(max_length=3, default=0)
    avatar = models.ImageField(upload_to='user/avatar', default=settings.STATIC_URL + 'img/default_avatar.png')

    class Meta:
        app_label = core.APP_LABEL

    def __unicode__(self):
        return self.name


class Event(models.Model):
    class Status:
        Scheduled = 0
        Held = 1
        Cancelled = 2

    name = models.CharField(max_length=255)
    description = models.TextField()
    city = models.ForeignKey(City)
    date = models.DateField()
    owner = models.ForeignKey(User, related_name='organized_events')
    cost = models.IntegerField()
    current_sum = models.IntegerField(default=0)
    current_sum_for_user = models.IntegerField(default=0)
    max_participants_count = models.IntegerField(null=True)
    participants = models.ManyToManyField(User, related_name='participate_in_events', through='Participation')
    status = models.IntegerField(default=Status.Scheduled)
    logo = models.ImageField(upload_to='event/logo/', default=settings.STATIC_URL + 'img/event_logo.gif')
    strategy= models.ForeignKey(Strategy, default=Strategy.default_value)

    class Meta:
        app_label = core.APP_LABEL

    def __unicode__(self):
        return self.name


class Discussion(models.Model):
    name = models.CharField(max_length=255)
    event = models.ForeignKey(Event, related_name='discussions')
    allow_guest_message = models.BooleanField(default=False)
    
    class Meta:
        app_label = core.APP_LABEL
        
    def __unicode__(self):
        return self.name


class Message(models.Model):
    author = models.ForeignKey(User, related_name='writer_of_messages')
    addressee = models.ForeignKey(User, related_name='adressee_of_messages')
    text = models.TextField()
    datetime = models.DateTimeField()
    discussion = models.ForeignKey(Discussion)
     
    class Meta:
        app_label = core.APP_LABEL
         
    def __unicode__(self):
        return self.author 


class Participation(models.Model):
    event = models.ForeignKey(Event)
    user = models.ForeignKey(User)
    investment = models.IntegerField()
    patron = models.BooleanField(default=False)

    class Meta:
        app_label = core.APP_LABEL

    def __unicode__(self):
        return '{0} {1} {2}'.format(self.event, self.user, self.investment)
    
    
from auth import Auth, AuthProvider
from core.models.auth.VkontakteCredential import VkontakteCredential
from core.models.auth.FacebookCredential import FacebookCredential
from core.models.auth.OpenIdCredential import OpenIdCredential
from core.models.auth.InternalCredential import InternalCredential